#region Header
//=============================================================================
//                          GShield (Game Shield)
//=============================================================================
//
// Original Programmer: Shawn Jackson
// Company: WaveTech Digital Technologies (www.wtdt.com)
// Copyright: (c) 2006 WaveTech Digital Technologies, All Rights Reserved.
// Assembly: Engine
// Code File Name: BannedWatcher.cs
// Code File Date: 12/8/2006
//
// Code File Info: Banned Watcher System
//=============================================================================
/*
 * UPDATES
 * -------
 *				ShawnJ 5/5/2007 (0.9.0.x): Refactoring for OSS.
 *				ShawnJ 5/24/2007 (0.9.2.x): Changed LogDAO to Logging System.
 */
#endregion Header

#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Threading;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using WaveTech.GShield.Data;
using WaveTech.GShield.Engine.Managers;
using WaveTech.GShield.Engine.Systems;
using WaveTech.GShield.Model;
using Exception=System.Exception;

#endregion

namespace WaveTech.GShield.Engine.Watchers
{
	public class BannedWatcher
	{
		private static Thread watcher;
		private static bool watcherActive;

		#region Constructor

		public BannedWatcher()
		{
			watcher = new Thread(Worker);
		}

		#endregion Constructor

		#region Start

		public void Start()
		{
			watcherActive = true;
			Logging.Write("The banned games watcher system has been started.", LogCategory.Banned, LogPriority.Low,
			              LogType.BannedStartStop);

			if (watcher != null)
			{
				watcher.Start();
			}
			else
			{
				watcher = new Thread(Worker);
				watcher.Start();
			}
		}

		#endregion Start

		#region Stop

		public void Stop()
		{
			watcherActive = false;
			Logging.Write("The banned games watcher system has been stopped.", LogCategory.Banned, LogPriority.Low,
			              LogType.BannedStartStop);

			if (watcher != null)
			{
				watcher.Abort();
				watcher = null;
			}
		}

		#endregion Stop

		#region Worker

		private void Worker()
		{
			try
			{
				Setting s = SettingsDAO.Get();

				while (watcherActive)
				{
					GameManager gm = GameManager.Instance;

					GameProcessCollection gameProcesses = ProcessHelper.Instance.GetAllGameProcesses();

					/*
					ProcessHelper pHelp = new ProcessHelper();

					GameProcessCollection gameProcesses = pHelp.GetDirectXProcesses();
					GameProcessCollection otherProcesses = pHelp.GetOtherProcesses();

					if (otherProcesses != null)
					{
						foreach (GameProcess process in otherProcesses)
						{
							if (gameProcesses == null)
								gameProcesses = new GameProcessCollection();

							gameProcesses.Add(process);
						}
					}
					*/

					if (gameProcesses != null && gameProcesses.Count > 0)
					{
						foreach (GameProcess p in gameProcesses)
						{
							Game g;
							g = gm.GetGame(p, p.FullPath, p.InternalName, p.ProductName, p.Company, p.Process.MainModule.FileName);

							if (g != null)
							{
								if (BannedDAO.IsGameBanned(g.Id))
								{
									Logging.Write("Killed a Banned Game (" + g.Name + ") using the SYSTEM policy, Id = " + g.Id,
									              LogCategory.Banned, LogPriority.High, LogType.BannedViolation,
									              ProcessHelper.Instance.GetOwnerForProcess(p.Id));

									Messaging.ShowBannedWarning();

									ProcessHelper.Instance.KillProcess(p.Id);
								}
								else if (BannedDAO.IsGameBanned(g.Id, ProcessHelper.Instance.GetOwnerForProcess(p.Id)))
								{
									Logging.Write("Killed a Banned Game (" + g.Name + ") using the users policy, Id = " + g.Id,
									              LogCategory.Banned, LogPriority.High, LogType.BannedViolation,
									              ProcessHelper.Instance.GetOwnerForProcess(p.Id));

									EmailManager.Instance.SendBannedViolationMessage(p.UserName, DateTime.Now, g.Name);

									Messaging.ShowBannedWarning();

									ProcessHelper.Instance.KillProcess(p.Id);
								}
							}
							else
							{
								Logging.Write("Game Unknown: " + p.Process.MainModule.FileName, LogCategory.Banned, LogPriority.High,
								              LogType.Banned, ProcessHelper.Instance.GetOwnerForProcess(p.Id));
							}
						}
					}

					if (s.BannedSleepTimer != null && int.Parse(s.BannedSleepTimer) > 0 && int.Parse(s.BannedSleepTimer) <= 3600000)
						Thread.Sleep(int.Parse(s.BannedSleepTimer));
					else
						Thread.Sleep(180000);
				}
			}
			catch (Exception e)
			{
				if (ExceptionPolicy.HandleException(e, "Engine Policy"))
					throw;
			}
		}

		#endregion Worker
	}
}